package email

import (
	"github.com/go-gomail/gomail"
	"strings"
)

type Options struct {
	MailHost string
	MailPort int
	MailUser string // 发件人
	MailPass string // 发件人密码
	MailTo   string // 收件人 多个用,分割
	Subject  string // 邮件主题
	Body     string // 邮件内容
}

func (options *Options) Send() error {
	message := gomail.NewMessage()
	message.SetHeader("From", options.MailUser)

	//可发送多个用户
	receivers := strings.Split(options.MailTo, ",")
	message.SetHeader("To", receivers...)
	message.SetHeader("Subject", options.Subject)
	message.SetBody("text/html", options.Body)

	dialer := gomail.NewDialer(options.MailHost, options.MailPort, options.MailUser, options.MailPass)
	err := dialer.DialAndSend(message)
	return err
}
